﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using LibFriesland_Campina_CRM;


namespace LibFriesland_Campina_CRM
{
    class DalManagerMonitoring
    {
        //Them ManagerMonitoring
        public static int addManagerMonitoring(BizManagerMonitoring pManagerMonitoring)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                ManagerMonitoring managerMonitoring = new ManagerMonitoring();
                managerMonitoring.DateTimeAction = pManagerMonitoring.datetime_action;
                managerMonitoring.DateTimeMonitoring = pManagerMonitoring.datetime_monitoring;
                managerMonitoring.ActionMonitoring = pManagerMonitoring.action_monitoring;
                managerMonitoring.CommentMonitoring = pManagerMonitoring.comment_monitoring;
                managerMonitoring.BySup = pManagerMonitoring.by_sup;
                managerMonitoring.ByManager = pManagerMonitoring.by_manager;
                db.ManagerMonitoring.InsertOnSubmit(managerMonitoring);
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        //Lay danh sach action cua Manager doi voi Sup
        public static List<BizManagerMonitoring> getSupMonitoringbyManager(DateTime pDatetime_tracking, string pBy_sup, string pBy_manager)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                //var managerMonitoringList = from item in db.ManagerMonitoring
                //                            where item.BySup == pBy_sup && item.ByManager == pBy_manager
                //                            select new BizManagerMonitoring(item.ManagerActionNumID, item.DateTimeAction, item.DateTimeMonitoring, item.ActionMonitoring, item.CommentMonitoring, item.BySup, item.ByManager);

                var managerMonitoringList = from item in db.ManagerMonitoring
                                            where item.BySup == pBy_sup
                                            select new BizManagerMonitoring(item.ManagerActionNumID, item.DateTimeAction, item.DateTimeMonitoring, item.ActionMonitoring, item.CommentMonitoring, item.BySup, item.ByManager);
                
                List<BizManagerMonitoring> results = managerMonitoringList.ToList();
                results = results.FindAll(
                                        delegate(BizManagerMonitoring item)
                                        {
                                            if (item.datetime_action == pDatetime_tracking)
                                                return true;
                                            else
                                                return false;
                                        });
                results.Sort(ManagerMonitoringSort.dateComparison);
                return results;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
